package com.hand.wyc24548.api.controller.v1;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.hzero.core.base.BaseController;
import org.hzero.core.util.Results;
import org.hzero.export.annotation.ExcelExport;
import org.hzero.export.vo.ExportParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.hand.wyc24548.app.service.SoHeaderService;
import com.hand.wyc24548.config.SwaggerApiConfig;
import com.hand.wyc24548.domain.entity.SoHeader;
import com.hand.wyc24548.domain.repository.SoHeaderRepository;

import io.choerodon.core.domain.Page;
import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @Author wangyc
 * @create 2019/8/8 14:17
 */

@Api(tags = SwaggerApiConfig.SOHEADER)
@RestController("soheader.v1")
@RequestMapping("/v1/soheader")
public class SoHeaderController extends BaseController {

    private final SoHeaderRepository soHeaderRepository;
    private final SoHeaderService soHeaderService;

    public SoHeaderController(SoHeaderRepository soHeaderRepository, SoHeaderService soHeaderService) {
        this.soHeaderRepository = soHeaderRepository;
        this.soHeaderService = soHeaderService;
    }

    @Permission(level = ResourceLevel.ORGANIZATION)
    @ApiOperation(value = "分页订单汇总查询数据")
    @GetMapping
    public ResponseEntity<Page<SoHeader>> selectSoHeader(SoHeader soHeader, PageRequest pageRequest){
        return Results.success(soHeaderRepository.selectSoHeader(soHeader,pageRequest));
    }

    @Permission(level = ResourceLevel.SITE)
    @ApiOperation(value = "根据订单ID查询订单明细")
    @GetMapping("/{soHeaderId}")
    public ResponseEntity<SoHeader> selectOrderId(@PathVariable @ApiParam(value = "订单ID") Long soHeaderId){
        return Results.success(soHeaderRepository.selectOrderId(soHeaderId));
    }

    @Permission(level = ResourceLevel.SITE)
    @ApiOperation(value = "新增订单头")
    @PostMapping
    public ResponseEntity<SoHeader> create(@RequestBody SoHeader soHeader){
        soHeader.setOrderDate(new Date());
        this.validObject(soHeader);  //校验数据
        return Results.success(soHeaderRepository.insertSoHeader(soHeader));
    }

    /**
     * 删除订单头行信息
     * @param soHeaderId
     * @return
     */
    @Permission(level = ResourceLevel.SITE)
    @ApiOperation(value = "删除订单头行信息")
    @DeleteMapping("/{soHeaderId}")
    private ResponseEntity<SoHeader> delectOrder(@PathVariable @ApiParam(value = "订单头ID") Long soHeaderId){
/*        SoHeader soHeader=new SoHeader();
        soHeader.setSoHeaderId(soHeaderId);
        SecurityTokenHelper.validToken(soHeader);*/
        soHeaderService.delete(soHeaderId);
        return Results.success();
    }

    @ApiOperation(value = "导出订单数据")
    @GetMapping("/export")
    @ExcelExport(SoHeader.class)
    public ResponseEntity export(SoHeader soHeader, ExportParam exportParam, HttpServletResponse response,PageRequest pageRequest){
        List<SoHeader> list=soHeaderRepository.export(soHeader,exportParam,pageRequest);
        return Results.success(list);
    }

}
